﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using PhotoGallery.Models;
using PhotoGallery.Repositories;
using System.Collections.Generic;
using System.Linq;

namespace PhotoGallery.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private PhotoGalleryRepository _repo;
        private IMemoryCache _cache;
        private const string _emptPhoto = "http://photogallery-1252912192.file.myqcloud.com/albumEmpty.png";


        public HomeController(ILogger<HomeController> logging,
            PhotoGalleryRepository repo,
            IMemoryCache cache)
        {
            _repo = repo;
            _logger = logging;
            _cache = cache;
        }

        /// <summary>
        /// 显示相册
        /// </summary>
        /// <param name="photoName">照片名称</param>
        /// <param name="albumType">0:公共,相册1:用户相册,其他值不限制</param>
        /// <returns></returns>
        public IActionResult Index(string photoName="",int albumType=2)
        {
            var alist = _cache.Get<List<AlbumInfoModel>>(CacheKeys.AllAlbumsKey);
            //List<PhotoInfoModel> photos = null;
            int photoTotal = 0;

            if (alist == null)
            {
                var query = _repo.Albums.Where(w => w.IsPublic);
                if (albumType<2)
                {
                    query = query.Where(w => w.AlbumType == albumType);
                }
                
                alist =  query.ToList();
                if(alist == null || alist.Count < 1)
                {
                    return View(new List<AlbumInfoModel>());
                }
                //var query2 = _repo.Photos.Where(w => w.AlbumName != "" );
                for (var i = 0; i < alist.Count; i++)
                {
                    var albumName = alist[i].Name;
                    if (string.IsNullOrEmpty(alist[i].CoverPhoto))
                    {
                        alist[i].CoverPhoto = _emptPhoto;
                    }
                    var query2 = _repo.Photos.Where(w => w.AlbumName == albumName && w.UploadUser == alist[i].Owner);
                    if (!string.IsNullOrWhiteSpace(photoName))
                    {
                        query2 = query2.Where(w => w.Name.Contains(photoName));
                    }
                    alist[i].Photos = query2.ToList();
                    photoTotal += alist[i].Photos.Count;
                    
                }
            }
            ViewData["Description"] = $"My photo gallery with {alist.Count} albums";
            ViewData["Image"] = Request.Scheme + "://" + Request.Host + alist.FirstOrDefault()?.CoverPhoto;
            //var model = _ac.Albums.Where(w => w.IsPublic == true && w.AlbumType==0).ToList();
            if (!string.IsNullOrWhiteSpace(photoName))
            {
                ViewBag.Msg= $"符合搜索条件图片共有{photoTotal}张";
            }
            
            return View(alist);
        }


        public IActionResult About()
        {
            return View();
        }
    }
}